package pearson.automation.assets;

import org.openqa.selenium.By;
import org.openqa.selenium.interactions.Actions;

import pearson.automation.forms.note.NoteDeleteDialog;
import pearson.automation.forms.note.NoteDetailsForm;

import framework.BaseEntity;
import framework.elements.Button;
import framework.elements.Label;

public class Note extends BaseEntity {

    private Label sectionNumber = null;
    private Label title = null;
    private Label author = null;
    private Button view = null;
    private Button delete = null;

    @Override
    protected String formatLogMsg(String message) {
        return "Note";
    }

    public Note(String xPathTemplate) {
        sectionNumber = new Label(By.xpath(String.format("%s//%s",
                xPathTemplate, "span[@class='section-number']")), "Section");
        title = new Label(By.xpath(String.format("%s//%s",
                xPathTemplate, "span[@class='title']")), "Title");
        author = new Label(By.xpath(String.format("%s//%s",
                xPathTemplate, "span[@class='author']")), "Author");
        view = new Button(By.xpath(String.format("%s//%s",
                xPathTemplate, "a[@class='view-link']")), "View");
        delete = new Button(By.xpath(String.format("%s//%s",
                xPathTemplate, "a[@class='delete-link']")), "Delete");
    }
    
    public String getSectionNumber() {
        return sectionNumber.getText();
    }
    
    public String getTitle() {
        return title.getText();
    }
    
    public String getAuthor() {
        Actions builder = new Actions(browser.getDriver());
        builder
            .moveToElement(title.getElement())
            .build()
            .perform();
        return author.getText();
    }

    public NoteDetailsForm clickView() {
        clickViewNoDetailsForm();
        return new NoteDetailsForm();
    }
    
    public void clickViewNoDetailsForm() {
        Actions builder = new Actions(browser.getDriver());
        builder
            .moveToElement(title.getElement())
            .moveToElement(browser.getDriver().findElement(view.getLocator()))
            .click(browser.getDriver().findElement(view.getLocator()))
            .build()
            .perform();
    }
    
    public NoteDeleteDialog clickDelete() {
        Actions builder = new Actions(browser.getDriver());
        builder
            .moveToElement(title.getElement())
            .moveToElement(browser.getDriver().findElement(delete.getLocator()))
            .click(browser.getDriver().findElement(delete.getLocator()))
            .build()
            .perform();
        return new NoteDeleteDialog();
    }
}
